package com.wkbb.basic.service.impl;

import com.wkbb.basic.mapper.DistrictMapper;
import com.wkbb.basic.model.District;
import com.wkbb.basic.service.DistrictService;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.constants.RedisKeyConstant;
import com.wkbb.common.dto.basic.DistrictDto;
import com.wkbb.common.redis.RedisTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author luowenyan
 * @version V1.0
 * @className DistrictServiceImpl
 * @description
 * @date 2019/7/29 10:51
 */
@Service
@Slf4j
public class DistrictServiceImpl extends BaseServiceImpl<District> implements DistrictService {

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    @Override
    public List<DistrictDto> getDistrictList(Integer parentCode,Integer type) {
        //先从redis中查询,查询不到,再从数据库中查询
        List<DistrictDto> redisDistrictList = (List<DistrictDto>) redisTemplateUtil.get(RedisKeyConstant.WKBB_BASIC_DICT+"district:"+parentCode+"_"+type);
        if(null!=redisDistrictList){
            return redisDistrictList;
        }
        List<DistrictDto> districtList =  districtMapper.getDistrictList(parentCode,type);
        redisTemplateUtil.set(RedisKeyConstant.WKBB_BASIC_DICT+"district:"+parentCode+"_"+type,districtList,RedisKeyConstant.EXPIRATION_TIME_1_MONTH);
        return districtList;
    }

    @Override
    public DistrictDto findDistrict(String name, Integer type) {
        return districtMapper.findDistrict(name,type);
    }
}
